<template>
  <div>
    <div>
      <el-button
        v-waves
        class="filter-item"
        style="margin-left: 10px;"
        type="primary"
        icon="el-icon-edit"
        @click="handleCreate"
      >
        增加
      </el-button>
    </div>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="id" prop="id" sortable="custom" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="用户昵称"
        prop="nick_name"
        sortable="custom"
        align="center"
        width="80"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.user.nickName }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="商品名称"
        prop="title"
        sortable="custom"
        align="center"
        width="120"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.product.title }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="商品图片"
        prop="pic"
        sortable="custom"
        align="center"
        width="180"
      >
        <template slot-scope="scope">
          <img width="80" :src="scope.row.product.pic">
        </template>
      </el-table-column>
      <el-table-column
        label="商品价格"
        prop="price"
        sortable="custom"
        align="center"
        width="80"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.product.price }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="商品数量"
        prop="count"
        sortable="custom"
        align="center"
        width="80"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.count }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="商品总价"
        prop="totalPrice"
        sortable="custom"
        align="center"
        width="100"
      >
        <template slot-scope="scope">
          <span>{{ (scope.row.count)*(scope.row.product.price) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="购物车生成日期"
        prop="createDate"
        sortable="custom"
        align="center"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.createDate }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" width="220" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button
            v-if="row.status!='deleted'"
            size="mini"
            type="danger"
            @click="handleDel(row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>

    </el-table>
    <el-dialog
      :title="textMap[dialogStatus]"
      :visible.sync="dialogFormVisible"
    >
      <el-form
        ref="dataForm"
        :rules="rules"
        :model="temp"
        label-position="left"
        label-width="110px"
        style="width: 400px; margin-left:50px;"
      >
        <el-form-item label="驾驶员姓名" prop="name">
          <el-input v-model="temp.name" />
        </el-form-item>
        <el-form-item label="驾驶员所属公司名称" prop="compnay">
          <el-input v-model="temp.company" />
        </el-form-item>
        <el-form-item label="驾驶员手机号码" prop="tel">
          <el-input v-model="temp.tel" />
        </el-form-item>
        <el-form-item label="登陆密码" prop="pwd">
          <el-input v-model="temp.pwd" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">
          取消
        </el-button>
        <el-button type="primary" @click="dialogStatus==='create'?createData():updateData()">
          确定
        </el-button>
      </div>
    </el-dialog>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />

  </div>
</template>

<script>
import $ from 'jquery'
import ajax from '@/utils/baseurl'
import Pagination from '@/components/Pagination'
import waves from '@/directive/waves'

var p

export default {

  name: 'Driver',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      temp: {
        title: '',
        pic: '',
        nickName: '',
        createDate: '',
        count: '',
        price: '',
        id: ''
      },
      rules: {
        name: [{ required: true, message: '驾驶员信息不能为空', trigger: 'change' }],
        tel: [{ required: true, message: '手机信息不能为空', trigger: 'change' },
          { pattern: /^1[3-9]\d{9}$/, trigger: 'blur', message: '号码不规范'
          }],
        pwd: [{ required: true, message: '密码不能为空', trigger: 'change' }],
        company: [{ required: true, message: '公司名称不能为空', trigger: 'change' }]
      },
      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      listQuery: {
        page: 1,
        limit: 10,
        sort: '+id'
      },
      total: 0,
      tableKey: 0,
      listLoading: true,
      list: null
    }
  },
  created() {
    p = this
    // console.log(ajax);
    this.getList()
  },
  methods: {
    updateData() {
      this.createData()
    },
    resetTemp() {
      this.temp = {
        name: '',
        tel: '',
        company: '',
        pwd: ''
      }
    },
    createData() {
      var p = this
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          ajax('driver/add', this.temp, function(data) {
            if (data.id) {
              p.dialogFormVisible = false
              p.getList()
              p.$notify({
                title: '完成',
                message: p.dialogStatus == 'create' ? '新增成功' : '修改成功',
                type: 'success',
                duration: 2000
              })
            }
          })
        }
      })
    },
    handleCreate() {
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    handleUpdate(row) {
      this.temp = Object.assign({}, row)
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    handleDel(row) {
      var p = this
      this.$confirm('此操作将永久删除该条信息, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        ajax('delShopCar', { id: row.id }, function(data) {
          if (data) {
            p.getList()
            p.$message({
              type: 'success',
              message: '删除成功!'

            })
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    getList() {
      this.listLoading = true
      ajax('getAllShopCar', this.listQuery, function(data) {
        // console.log(data);
        p.list = data
        p.total = data.totalElements
        p.listLoading = false
      })
    }

  }
}
</script>

=======

  </div>
</template>
>>>>>>> Stashed changes
